import dayjs from 'dayjs';
import Decimal from 'decimal.js';

type FormatDateType = 'date' | 'time' | null;

const formatAreaNames = (names: string) => {
  return names ? names?.split(',')?.join('-') : '-';
};

const formatAmount = (value: number | string) => {
  if (Number.isNaN(Number(value))) {
    return '-';
  }

  let tpMoney = new Decimal(value).toFixed(2);

  tpMoney = tpMoney.replace(/\B(?=(\d{3})+(?!\d))/g, ',');

  return tpMoney;
};

const formatDate = (value: string | Date, type?: FormatDateType, format?: string) => {
  if (!value) {
    return '-';
  }

  if (format) {
    return dayjs(value).format(format);
  }

  if (type === 'date') {
    return dayjs(value).format('YYYY-MM-DD');
  } else if (type === 'time') {
    return dayjs(value).format('YYYY-MM-DD HH:mm:ss');
  }

  return dayjs(value).format('YYYY-MM-DD');
};

export { formatAreaNames, formatAmount, formatDate };
